import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions

from common.base_page import BasePage
from page.edit_page import editpage


class EmplistPage(BasePage):
    _EMP_ADDRESS_BOOK = (By.XPATH, "//*[text()='通讯录']")
    _ADDEMP = (By.XPATH, "//a[@class='qui_btn ww_btn js_add_member']")
    __MEMBER_LIST_INFO = (By.CSS_SELECTOR, '.member_colRight_memberTable_td:nth-child(2)')
    _PHONE_LIST = (By.XPATH, "//tr//td[5]")
    _DELETE=(By.XPATH, "//*[text()='删除']")
    _DELETE_CONFIRM=(By.XPATH, "//a[@class='qui_btn ww_btn ww_btn_Blue']")

    def go_to_edit(self):
        with allure.step("进入编辑成员页面"):
            """进入编辑成员页面"""
            # 等待添加按钮可点击
            self.wait_until(expected_conditions.element_to_be_clickable(self._EMP_ADDRESS_BOOK))
            # 点击添加成员
            self.find_eles(self._ADDEMP)[2].click()
            return editpage(self.driver)

    def get_emp_list_info(self):
        with allure.step("获取员工手机号列表"):
            """返回成员列表手机号唯一标识"""
            # 等待列表出现
            self.wait_until(expected_conditions.element_to_be_clickable(
                self.__MEMBER_LIST_INFO))

            # 获取成员手机号列表
            phones = self.find_eles(self._PHONE_LIST)
            phone_list = []
            for phone in phones:
                phone_list.append(phone.text)

        return phone_list

    def del_emp(self, phone):
        with allure.step("删除添加数据"):
            self.driver.implicitly_wait(3)
            self.wait_until(expected_conditions.visibility_of_element_located((By.XPATH, f"//span[text()='{phone}']/../..//input"))).click()
            self.find_ele(self._DELETE).click()
            self.wait_until(expected_conditions.visibility_of_element_located(self._DELETE_CONFIRM)).click()
